[TOC]

Creating Arrays

This page introduces some commonly used methods to create arrays.

Catenation

Let us start with an example. Suppose you want to create a row vector a, which contains 1, 2 and 3. Then, you can use a = [1 2 3]. Here, two numbers are separated by a whitespace, so that they are catenated horizontally. Instead of a whitespace, you can also use a comma. For example, a = [1, 2, 3] gives the same row vector.

When two numbers are separated by a semicolon, they are catenated vertically to form a column vector. For example, b = [1; 2; 3] is a column vector of 3 numbers, and d = [1, 3; 2 4] is a array (two row vectors catenated vertically).

The catenation method is not limited to numbers, it also works for arrays. In the following example, the array c is obtained by vertically catenating a and b. Whereas, the row vector d is obtained by horizontally catenating a and b.

Input
a = [1, 3];
b = [2, 4];
c = [a; b]	% which gives a 2 x 2 array
d = [a b]		% which gives [1 3 2 4]
Output
c = 
 1.0000   3.0000
 2.0000   4.0000

d = 
 1.0000   3.0000   2.0000   4.0000

Be sure that the arrays to be catenated horizontally (vertically) have the same number of rows (columns). Otherwise, a size-mismatch error will be thrown:

Input
a = [1, 3]		% 2 columns
b = [2; 4]		% 1 columns
c = [a; b]		% size mismatch.
Output
a = 
 1.0000   3.0000

b = 
 2.0000
 4.0000

Error at Line 4(5). Dimension lengths mismatch.

More illustrative examples are shown below.

Input
[[], []]									% gives an empty array
[2 []]										% gives [2]
[[1 2 3 4]; [[5 6] 7 8]]	% Nested catenation works!
Output
ans =
 [] (double array)

ans = 
 2.0000

ans = 
 1.0000   2.0000   3.0000   4.0000
 5.0000   6.0000   7.0000   8.0000

Creating Vectors

The colon : operator and the function linspace are used frequently to create vectors.

The Colon Operator

Vectors of forms, such as and , can be created using the colon : operator.

Input
1:10
1:2:11
Output
ans = 
Columns 1 through 7:
 1.0000   2.0000   3.0000   4.0000   5.0000   6.0000   7.0000
Columns 8 through 10:
 8.0000   9.0000   10.000

ans = 
 1.0000   3.0000   5.0000   7.0000   9.0000   11.000

This is how the colon operator : works. Assume that b is positive and a < c. The statement a:b:c starts with a and keeps adding b to it until the sum reaches the biggest number not exceeding c.

In the following example, the last number in the vector is 9, not 11, because exceeds 10.

Input
1:2:10
Output
ans = 
 1.0000   3.0000   5.0000   7.0000   9.0000

Assume that b is positive and a > c. The statement a:-b:c starts with a and keeps subtracting b from it until it is reaches the smallest number not smaller than c. For example, can be obtained by

Input
10:-1:1
Output
ans = 
Columns 1 through 7:
 10.000   9.0000   8.0000   7.0000   6.0000   5.0000   4.0000
Columns 8 through 10:
 3.0000   2.0000   1.0000

The following vector excludes 1, because is smaller than 3.

Input
10:-2:3
Output
ans = 
 10.000   8.0000   6.0000   4.0000

The statement a:b:c gives an empty vector in the following cases:

For examples, each of the following gives an empty vector:

10:2:1
1:-1:10
1:0:10

linspace

We use linspace(a,b,N) to create a vector of N numbers ranging from a to b. If N is not specified, it returns a vector of 100 numbers.

Here are some examples:

Input
% A vector of 10 number in ascending order.
linspace(1,2,10)
% A vector of 11 numbers in descending order.
linspace(2,1,11)
Output
ans = 
Columns 1 through 7:
 1.0000   1.1111   1.2222   1.3333   1.4444   1.5556   1.6667
Columns 8 through 10:
 1.7778   1.8889   2.0000

ans = 
Columns 1 through 7:
 2.0000   1.9000   1.8000   1.7000   1.6000   1.5000   1.4000
Columns 8 through 11:
 1.3000   1.2000   1.1000   1.0000

More specifically, linspace(a,b,n) returns the vector , where for the spacing is defined as

For more details, see the document page for linspace.

Constant Arrays

The following functions create constant arrays. For more details, see the respective document pages.

Random Arrays

The following functions create arrays of random numbers. For more details, see the respective document pages.